#!/usr/bin/python
# -*- coding: UTF-8 -*-

'''
用于获取省市区地址信息
来源：http://www.mca.gov.cn/article/sj/tjbz/a/2017/201801/201801151447.html
'''


import requests
from lxml import etree
import pymysql
import time



def saveData(data):

    count = 0
    conn = pymysql.connect(host='127.0.0.1', user='root', password='123456', db='test',charset="utf8")

    cur = conn.cursor()

    for code, name in data.items():
        t = time.time()

        # print(g)

        sql = '''
        insert into  admin_area(type,code,name,createTime,updateTime,deleted)
        values(%s,%s,%s,%s,%s);
       '''
        try:
            nowTime = int(round(t * 1000))
            if code[4] == '0':
                type = 
            B = cur.execute(sql,(code,name,nowTime,nowTime,'0' ))
            conn.commit()
            print('成功')
        except  Exception as e:
            print(e)

    conn.commit()
    cur.close()
    conn.close()


url = 'http://www.mca.gov.cn/article/sj/tjbz/a/2017/201801/201801151447.html'
headers={
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"}
print(url)
data = requests.get(url, headers=headers).text
s=etree.HTML(data)
#解析数据
print(s)
resultcode = s.xpath("//*/td[@class='xl7026226'][1]/text()")
resultname = s.xpath("//*/td[@class='xl7026226'][2]/text()")
print(len(resultcode))
print(len(resultname))

result = zip(resultcode, resultname)
resultDict = dict( (name,value) for name,value in result)
# print(resultDict)

testData = {'110000' : '北京市'}


saveData(resultDict)

